package com.tiger.web.controller.project;

import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.AssetMonth;
import com.tiger.project.service.IAssetMonthService;
import com.tiger.project.service.IAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 【资产列表】Controller
 *
 * @author standard
 * @date 2022-09-15
 */
@Api(tags = "资产列表")
@RestController
@RequestMapping("/system/asset")
public class AssetController extends BaseController {
    @Autowired
    private IAssetService assetService;

    @Autowired
    private IAssetMonthService assetMonthService;

    /**
     * 查询【资产列表】列表
     */
    @ApiOperation(value = "查询【资产列表】列表")
    //@PreAuthorize("@ss.hasPermi('system:asset:list')")
    @GetMapping("/list")
    public TableDataInfo list(Asset asset) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        asset.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<Asset> list = assetService.selectAssetList(asset);
        return getDataTable(list);
    }

    /**
     * 房屋空置天数排名： 排前十，无关联合同不排，且可租面积>0
     */
    @ApiOperation(value = "房屋空置天数排名")
    @PostMapping("/vacantHouse")
    public TableDataInfo vacantHouse() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        List<Map<String, Object>> list = assetService.getVacantHouseList(loginUser.getUser().getDeptId());
        return getDataTable(list);
    }

    /**
     * 总资产收益率: 总租金/可出租资产资产总价值（房屋价值）
     */
    @ApiOperation(value = "总资产收益率")
    @PostMapping("/returnOnTotalAssets")
    public AjaxResult returnOnTotalAssets() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        Map<String, Object> info = assetService.getReturnOnTotalAssets(loginUser.getUser().getDeptId());
        return AjaxResult.success(info);
    }

    /**
     *  资产分类
     */
    @ApiOperation(value = "资产分类")
    @PostMapping("/getAssetsCategory")
    public AjaxResult getAssetsCategory() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        List<Map<String, Object>> info = assetService.getAssetsCategory(loginUser.getUser().getDeptId());
        return AjaxResult.success(info);
    }

    /**
     *  近12个月的资产状态月度趋势
     */
    @ApiOperation(value = "近12个月的资产状态月度趋势")
    @PostMapping("/getAssetMonthList")
    public AjaxResult getAssetMonthList() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(assetMonthService.getAssetMonthList(loginUser.getUser().getDeptId()));
    }

    /**
     * 查询资产面积和资产价值
     */
    @ApiOperation(value = "查询资产面积和资产价值")
    @PostMapping("/getAssetAreaAndValue")
    public AjaxResult getAssetAreaAndValue() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(assetService.getAssetAreaAndValue(loginUser.getUser().getDeptId()));
    }


    /**
     * 新增【资产列表】
     */
    @ApiOperation(value = "新增【资产列表】")
    //@PreAuthorize("@ss.hasPermi('system:asset:add')")
    @Log(title = "新增【资产列表】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Asset asset) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        asset.setCompanyId(loginUser.getUser().getDeptId());
        return toAjax(assetService.insertAsset(asset));
    }

    /**
     * 修改【资产列表】
     */
    @ApiOperation(value = "修改【资产列表】")
    //@PreAuthorize("@ss.hasPermi('system:asset:edit')")
    @Log(title = "修改【资产列表】", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Asset asset) {
        return toAjax(assetService.updateAsset(asset));
    }


    /**
     * 删除【资产列表】
     */
    @ApiOperation(value = "删除【资产列表】")
    //@PreAuthorize("@ss.hasPermi('system:asset:remove')")
    @Log(title = "删除【资产列表】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assetService.deleteAssetByIds(ids));
    }

    /**
     * 获取【资产列表】详细信息
     */
    @ApiOperation(value = "获取【资产列表】列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:asset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(assetService.selectAssetById(id));
    }
}
